package com.webwork.mood.record.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.webwork.mood.common.dao.impl.SimpleDao;
import com.webwork.mood.record.dao.DrawInfoDao;
import com.webwork.mood.record.dataobject.DrawInfo;
import com.webwork.mood.util.ColumnLimit;
import com.webwork.mood.util.DBSimpleSQL;
import com.webwork.mood.util.StrManager;

@Repository
public class DrawInfoDaoImpl extends SimpleDao implements DrawInfoDao {

	public DrawInfoDaoImpl() {
		super();
		this.dbTable = "t_draw_info";
		dbColumns.clear();
		dbColumns.put("userId", "USER_ID");
		dbColumns.put("awardName", "AWARD_NAME");
		dbColumns.put("patchCount", "PATCH_COUNT");
	}
	
	@Override
	public ColumnLimit condition(Object object) {
		if (!(object instanceof DrawInfo))
			return new ColumnLimit();
		DrawInfo o = (DrawInfo) object;
		ColumnLimit ret = new ColumnLimit();
		if (!StrManager.isEmpty(o.getUserId())) {
			ret.add(dbColumns.get("userId") + " = ");
		}
		if (!StrManager.isEmpty(o.getAwardName())) {
			ret.add(dbColumns.get("awardName") + " = ");
		}
		return ret;
	}

	@Override
	public Object[] statement(Object object) {
		DrawInfo o = (DrawInfo) object;
		List<Object> ret = new ArrayList<Object>();
		if (!StrManager.isEmpty(o.getUserId())) {
			ret.add(o.getUserId());
		}
		if (!StrManager.isEmpty(o.getAwardName())) {
			ret.add(o.getAwardName());
		}
		return ret.toArray();
	}

	@Override
	public List<Object> pushObject(Object object) {
		DrawInfo o = (DrawInfo) object;
		List<Object> objs = new ArrayList<Object>();
		objs.add(o.getUserId());
		objs.add(o.getAwardName());
		objs.add(o.getPatchCount());
		return objs;
	}

	@Override
	public Object catchObject(Map<String, Object> map) {
		DrawInfo ret = new DrawInfo();
		ret.setUserId((String) map.get(dbColumns.get("userId")));
		ret.setAwardName((String) map.get(dbColumns.get("awardName")));
		ret.setPatchCount((Integer) map.get(dbColumns.get("patchCount")));
		return ret;
	}
	
	@Override
	public void insert(DrawInfo o) {
		String sql = DBSimpleSQL.insert(dbTable, dbColumns.size());
		this.getJdbcTemplate().update(sql, this.pushObject(o).toArray());
	}

	@Override
	public void update(DrawInfo o) {
		ColumnLimit limit = new ColumnLimit();
		limit.add(dbColumns.get("userId") + " = ");
		limit.add(dbColumns.get("awardName") + " = ");
		String sql = DBSimpleSQL.update(dbTable, dbColumns, limit);
		List<Object> objs = this.pushObject(o);
		objs.add(o.getUserId());
		objs.add(o.getAwardName());
		this.getJdbcTemplate().update(sql, objs.toArray());
	}

	@Override
	public List<DrawInfo> query(DrawInfo o, int page, int size) {
		List<DrawInfo> ret = new ArrayList<DrawInfo>();
		String sql = DBSimpleSQL.selectAll(dbTable, this.condition(o), page, size);
		List<Map<String, Object>> maps = this.getJdbcTemplate().queryForList(sql, this.statement(o));
		for (Map<String, Object> map : maps) {
			ret.add((DrawInfo) this.catchObject(map));
		}
		return ret;
	}

	@Override
	public int count(DrawInfo o) {
		String sql = DBSimpleSQL.count(dbTable, this.condition(o));
		return this.getJdbcTemplate().queryForObject(sql, this.statement(o), Integer.class);
	}
	
}
